﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Support.DTO;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess
{
    public class TipoOrdenDAL : BaseSQLDAL
    {
        public List<TipoOrdenDTO> GetTiposOrden()
        {
            List<TipoOrdenDTO> listaTipos = new List<TipoOrdenDTO>();

            try
            {

                CreateCommand("TipoOrden_Listado", true);

                ExecuteReader();
                while (Read())
                {
                    listaTipos.Add(FillDTO(GetDataReader()));
                }
                return listaTipos;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }
     
        public string GuardarTipoOrden(TipoOrdenDTO to)
        {
            try
            {
                CreateCommand("TipoOrden_Insertar", true);

                AddCmdParameter("@Nombre", to.Nombre, ParameterDirection.Input);
                AddCmdParameter("@Precio", to.Precio, ParameterDirection.Input);

                ExecuteReader();
                Read();
                return Convert.ToString(GetOutResult("msj"));

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }

        public List<TipoOrdenDTO> GetOrdenesByTipoPlanId(int tipoPlanId)
        {
            List<TipoOrdenDTO> ordenes = new List<TipoOrdenDTO>();

            try
            {

                CreateCommand("Orden_ObtenerPorPlanId", true);
                AddCmdParameter("@TipoPlanId", tipoPlanId, ParameterDirection.Input);

                ExecuteReader();
                while (Read())
                {
                    ordenes.Add(FillDTO(GetDataReader()));
                }
                return ordenes;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }

        private TipoOrdenDTO FillDTO(SqlDataReader reader)
        {
            int id = Convert.ToInt32(GetOutResult("IdTipoOrden"));
            string name = Convert.ToString(GetOutResult("Nombre")).Trim();
            string precio = Convert.ToString(GetOutResult("PrecioOrden"));
            return new TipoOrdenDTO()
            {
                Id = id,
                Nombre = name,
                Precio = precio
            };
        }

        public List<TipoOrdenDTO> BuscarTipoOrden(string filtro)
        {
            List<TipoOrdenDTO> ordenes = new List<TipoOrdenDTO>();

            try
            {

                CreateCommand("TipoOrden_Buscar", true);
                AddCmdParameter("@Filtro", filtro, ParameterDirection.Input);

                ExecuteReader();
                while (Read())
                {
                    ordenes.Add(FillDTO(GetDataReader()));
                }
                return ordenes;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }

        public bool EditarTipoOrden(TipoOrdenDTO tipoOrden)
        {
            try
            {
                CreateCommand("TipoOrden_Modificar", true);

                AddCmdParameter("@IdTipoOrden", tipoOrden.Id, ParameterDirection.Input);
                AddCmdParameter("@Nombre", tipoOrden.Nombre, ParameterDirection.Input);
                AddCmdParameter("@Precio", tipoOrden.Precio, ParameterDirection.Input);

                ExecuteReader();
                Read();
                return true;

            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }
    }
}
